package com.zhongli.devplatform.service.impl.teacher;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongli.devplatform.bean.Res;
import com.zhongli.devplatform.bean.SecurityHelper;
import com.zhongli.devplatform.component.FileUtil;
import com.zhongli.devplatform.dto.teacher.ReadOperationDTO;
import com.zhongli.devplatform.mapper.clazz.ReadClazzOperationMapper;
import com.zhongli.devplatform.mapper.operation.ReadOperationMapper;
import com.zhongli.devplatform.po.common.ReadClazzOperationPO;
import com.zhongli.devplatform.po.teacher.ReadOperationPO;
import com.zhongli.devplatform.service.imp.teacher.ReadOperationService;
import com.zhongli.devplatform.utils.BeanCopierUtil;
import com.zhongli.devplatform.vo.teacher.OperationVO;
import com.zhongli.devplatform.vo.teacher.TeacherOperationVO;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author aiyuan
 * @since 2020-07-30 16:52:18
 */
@Service
public class ReadOperationServiceImpl extends ServiceImpl<ReadOperationMapper, ReadOperationPO> implements ReadOperationService {

    @Resource
    private ReadOperationMapper operationMapper;

    @Resource
    private ReadClazzOperationMapper clazzOperationMapper;

    @Autowired
    private Scheduler scheduler;

    @Autowired
    private FileUtil fileUtil;

    @Override
    public List<OperationVO> getCurrentTeacherOperation(Integer currentUserId) {
        return operationMapper.getCurrentTeacherOperation(currentUserId);
    }

    /**
     * 发布作业保存 并保存班级和作业的三方表
     * @param operationDTO
     */
    @Override
    public void saveOpera(ReadOperationDTO operationDTO) {
        ReadOperationPO operationPO = BeanCopierUtil.copy(operationDTO, ReadOperationPO.class);
        if (StringUtils.hasLength(operationDTO.getImges())) {
            List<String> fileList = fileUtil.getFileList(operationDTO.getImges(), operationDTO.getFileName());
            if (fileList.size()>1){
                String imgs = fileList.stream().map(String::valueOf).collect(Collectors.joining(","));
                assert operationPO != null;
                operationPO.setImges(imgs);
            }else {
                assert operationPO != null;
                operationPO.setImges(fileList.get(0));
            }
        }
        saveOrUpdate(operationPO);
        if (operationDTO.getId() != null) {
            operationPO.setUpdateUserId(SecurityHelper.getCurrentUserId());
            operationPO.setUpdateTime(new Date());
            clazzOperationMapper.delete(new QueryWrapper<ReadClazzOperationPO>().eq("operation_id",operationPO.getId()));
        }
        //Integer gradesId = operationDTO.getGradeId();
        List<Integer> classIds = operationDTO.getClassIds();
        Integer currentUserId = SecurityHelper.getCurrentUserId();
        classIds.forEach(id->{
            ReadClazzOperationPO po = new ReadClazzOperationPO();
            po.setClazzId(id);
            po.setCreateUserId(currentUserId);
            po.setOperationId(operationPO.getId());
            clazzOperationMapper.insert(po);
        });


//        //定时任务
//        JobDetail jobDetail = JobBuilder
//                .newJob(OperationStatusUpdateTask.class)
//                .withIdentity("OperationTaskJob"+operationPO.getId())
//                .usingJobData("id",operationPO.getId())//传入当前作业id
//                .build();
//        //触发器
//        SimpleTrigger simple = TriggerBuilder.newTrigger()
//                .withIdentity("OperationTaskTrigger"+operationPO.getId())
//                .startAt(operationPO.getStartTime())
//                .withSchedule(
//                        SimpleScheduleBuilder.simpleSchedule()
//                                .withIntervalInSeconds(0)//每隔多少秒执行一次
//                                .withRepeatCount(0))//重复执行的次数
//                .build();
//        try {
//            scheduler.scheduleJob(jobDetail,simple);
//            scheduler.start();
//        } catch (SchedulerException e) {
//            e.printStackTrace();
//        }

    }

    /**
     * 定时任务 根据作业开始时间  来修改状态
     */
    @Override
    @Transactional
    public void operationStatusUpdate(Integer id) {
        ReadOperationPO readOperationPO = new ReadOperationPO();
        readOperationPO.setId(id);
        readOperationPO.setStatus(true);
        operationMapper.updateById(readOperationPO);
    }

    @Override
    public List<TeacherOperationVO> getTeacherOperation(Integer currentUserId,Integer status) {
        return operationMapper.getTeacherOperation(currentUserId, status);
    }

}
